// pages/home/subscribe/tradingArea/tradingArea.js
import api from "../../../../../common/api"
import locationBehavior from '../../../../../behaviors/location';
const cityMap = require('../../../../../utils/city')
import * as transferData from '../../../../../models/transferData'
Page({
  behaviors: [locationBehavior],

  /**
   * 页面的初始数据
   */
  data: {
    isLoading: true,
    cityName: '',
    cityCode: '',
    areaCurrent: [],
    areaList: [],
    collapseCurrent: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中...',
    })
    this.getCurrentCity()
      .then(() => {
        this.getAreaByCitycodes(this.data.cityCode)
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.hideShareMenu()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  // 确定订阅商圈
  onSubscribe() {
    if (!this.data.areaCurrent.length) {
      return
    }
    this.subscribeTradeareas({
        cityCode: this.data.cityCode,
        areaCode: this.data.collapseCurrent,
        tradArea: this.data.areaCurrent,
      })
      .then(() => {
        wx.navigateBack()
      })
      .catch((err) => {
        if (err === '30002') {
          wx.showToast({
            title: '该商圈暂无小区...',
            icon: 'none'
          })
        } else {
          wx.showToast({
            title: '订阅失败,请稍后再试...',
            icon: 'none'
          })
        }
      })
  },
  // 根据当前城市获取商圈
  getAreaByCitycodes(cityCode) {
    return new Promise((resolve, reject) => {
      this.setData({
        isLoading: true,
      })
      wx.showLoading({
        title: '加载中...',
      })
      api.getAreaByCitycodes(cityCode, (res) => {
        if (res && res.data && res.data.isSuccess && res.data.content && res.data.content.length) {
          let result = res.data.content.filter(item => {
            return item.hdBizcircles && item.hdBizcircles.length
          })
          this.setData({
            areaList: result
          })
          resolve()
        } else {
          reject()
        }
        this.setData({
          isLoading: false,
        })
        wx.hideLoading()
      })
    })
  },
  // 获取当前城市
  getCurrentCity() {
    return new Promise((resolve, reject) => {
      this.getCityBe(true)
        .then((city) => {
          let cityCode = ''
          cityMap.cityObjs.forEach(item => {
            if (item.city === city) {
              cityCode = item.code || ''
            }
          })
          if (cityCode) {
            this.setData({
              cityName: city,
              cityCode: cityCode,
            });
          } else {
            this.setData({
              cityName: '上海市',
              cityCode: '310000',
            });
          }
          resolve()
        })
        .catch(() => {
          this.setData({
            cityName: '上海市',
            cityCode: '310000',
          });
          resolve()
        })
    })
  },

  subscribeTradeareas({
    cityCode,
    areaCode,
    tradArea
  }) {
    return new Promise((resolve, reject) => {
      wx.showLoading({
        title: '订阅中...',
      })
      api.subscribeToBusiness({
        cityCode,
        areaCode,
        tradArea
      }, (res) => {
        wx.hideLoading()
        if (res && res.data && res.data.isSuccess) {
          transferData.setData('isRefreshSubscribeArea', true)
          resolve()
        } else {
          reject(res.data.errorCode)
        }
      }, (err) => {
        wx.hideLoading()
        reject()
      })
    })
  },
  // 判断展开的是哪一个面板
  onCollapsesChange(e) {
    this.setData({
      collapseCurrent: e.detail
    })
  },
  // 选择哪一个商圈
  onCheckboxsChange(e) {
    this.setData({
      areaCurrent: e.detail
    });
  },
  //  切换城市
  onCityChange(e) {
    this.setData({
      areaCurrent: [],
      areaList: [],
      collapseCurrent: [],
      cityName: e.detail.name,
      cityCode: e.detail.code,
    })
    this.getAreaByCitycodes(this.data.cityCode)
  },

})